﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Manul.Genetics.Genom
{
    public class GenomDirectory : GenomItem
    {
        public GenomDirectory()
        {
            IsSuperRoot = false;
        }
        public GenomDirectory(bool isSuperRoot)
        {
            IsSuperRoot = isSuperRoot;
        }
        public List<GenomItem> GenomItems = new List<GenomItem>();

        public bool IsSuperRoot
        { get; private set; }

        public override Gen FindGen(string code)
        {
            List<GenomItem> Genom = new List<GenomItem>();

            Gen resultGen = Gen.Null;
            foreach (GenomItem genomItem in GenomItems)
            {
                resultGen = genomItem.FindGen(code);
                if(resultGen != Gen.Null)
                    break;
            }
            return resultGen;
        }

        public override NamedItem FindByGuid(string guid)
        {
            NamedItem namedItem = base.FindByGuid(guid);
            if (namedItem != null)
                return namedItem;
            foreach (GenomItem genomItem in GenomItems)
                if ((namedItem = genomItem.FindByGuid(guid)) != null)
                    return namedItem;
            return null;
        }

        public override void Swap(int index1, int index2)
        {
            if (IsInList(index1) && IsInList(index2))
            {
                GenomItem genomItem = GenomItems[index1];
                GenomItems.RemoveAt(index1);
                GenomItems.Insert(index2, genomItem);
            }
        }
        public override bool IsInList(int index)
        {
            return index >= 0 && index < GenomItems.Count;
        }
    }
}
